package server

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"github.com/gin-gonic/gin"
)

type Config struct {
	Notice struct {
		Runtime    string `json:"Runtime"` //定时运行时间
        Requesturl string `json:"requesturl"` //请求url地址 http://xxxx/xxx
        Token string `json:"token"`
	} `json:"notice"`
    Mail struct {
        Smtphost string `json:"smtphost"`  //邮箱地址
        Port int `json:"port"` //邮箱短裤
        Username string `json:"username"` //邮箱账号
        Password string `json:"password"` //邮箱密码
        From string `json:"from"` //来自地址
    } `json:"mail"`
	Database struct {
		Host     string `json:"host"`
		Port     int    `json:"port"`
		Username string `json:"username"`
		Password string `json:"password"`
		DBName   string `json:"dbname"`
	} `json:"database"`
	Server struct {
		Port    int `json:"port"`
		Timeout int `json:"timeout"`
        Expirdays int `json:"expirdays"`
	} `json:"server"`
}

var Dconfig Config
func HTTPMain(JsonFile string){

	file, err := os.Open(JsonFile)
	if err != nil {
		fmt.Println("打开配置文件失败:", err)
		return
	}
	defer file.Close()
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&Dconfig)
	if err != nil {
		fmt.Println("解析配置文件失败:", err)
		return
	}

    hserver = gin.Default() //创建gin
    Port = fmt.Sprintf(":%d", Dconfig.Server.Port)
    // dbconfig := Dbinit{username: Dconfig.Database.Username,password:Dconfig.Database.Password,port:Dconfig.Database.Port,ipaddress:Dconfig.Database.Host,dbname:Dconfig.Database.DBName}
    // mysqldb = dbconfig.dbconnect()
	dbConnectinit()
	// fmt.Println(dbconfig.ipaddress)
	hserver.Handle(http.MethodGet,"/get/license/expir",Queryexpir) //验证
	hserver.Handle(http.MethodGet,"/get/user/info",GetInfo) //验证 已添加header Authorization 验证
	hserver.Handle(http.MethodGet,"/get/license/all",LicenseList) //验证
	hserver.Handle(http.MethodPost,"/update/license",LicenseUpdate) //验证
	hserver.Handle(http.MethodDelete,"/delete/license",LicenseDelete) //验证
	hserver.Handle(http.MethodPost,"/add/license",LicenseAdd) //验证
	hserver.Handle(http.MethodGet,"/get/rsa",GetRsa) //获取私钥或加密token
	hserver.Handle(http.MethodPost,"/add/user",CreateUser) //创建用户接口
    hserver.Run(Port)
}